package pro.softzhang.algo.lc600;

import pro.softzhang.algo.flag.difficulty.DRed;

/**
 *
 */
@DRed
public class LC647_PalindromicSubstrings {
    public static void main(String[] args) {
        String str = "abc";
        int count = new Solution().countSubstrings(str);
        System.out.println(count);
    }

    static
    class Solution {
        public int countSubstrings(String s) {
            if(s == null || s.length() == 0) {
                return 0;
            }

            int count = 0;
            for (int i = 0; i < s.length(); i++) {
                count += countPalindrome(s, i, i);
                count += countPalindrome(s, i, i + 1);
            }
            return count;
        }

        private int countPalindrome(String str, int start, int end) {
            int count = 0;
            while (start >= 0 && end < str.length() && str.charAt(start) == str.charAt(end)) {
                count++;
                start--;
                end++;
            }
            return count;
        }
    }
}
